import { AppModel } from '../../../../model/AppModel';
import { createUIWidgetMenu } from '../BlockPackage'
import Widget from '../../../../../beui/asm.js/ts/widgets/Widget';

export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.operators.primary,
        Blockly.Colours.operators.secondary,
        Blockly.Colours.operators.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model: AppModel) {
        this.appendDummyInput()
            .appendField("UI")
            .appendField(createUIWidgetMenu(model), 'wid')
            .appendField(new Blockly.FieldDropdown(() => {
                return [
                    ['颜色' , 'setColor'] ,
                    ['背景颜色' , 'setBgColor'] ,
                    ['层叠' , 'setZ'] ,
                ]
            }), 'style')
        this.appendValueInput("what")
    } ,

    run (args: any, util: any, runtime: any, model: AppModel) {
        let widget: Widget = model.ui.widget(args.wid)
        if(widget && typeof(widget[args.style])=='function') {
            widget[args.style] (args.what.toString() )
        }
        return true
    } ,

    coder: {
        JavaScript(block: any, coder: any, model: AppModel) {
            let wid = block.getFieldValue('wid')
            let widget = model.ui.mapWidgets[wid]
            if(!widget) {
                return ""
            }
            let method = block.getFieldValue('style')
            let content = coder.valueToCode(block, "what", coder.ORDER_ASSIGNMENT) || 'undefined'
            return `${coder.indentString()}be.ui.set("${widget.name}", "${method}", ${content})\n`
        }
    }
}
